package com.face.facerecognition.controller;

import com.alibaba.fastjson.JSONObject;
import com.face.facerecognition.entity.AuthTokenStatus;
import com.face.facerecognition.service.AuthTokenStatusService;
import com.face.facerecognition.service.BaiDuService;
import com.face.facerecognition.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/baidu")
public class BaiDuController {
    @Autowired
    private BaiDuService baiDuService;
    @Autowired
    private AuthTokenStatusService authTokenStatusService;
    /**
     *userMap包含 字段 id_name，id_no
     * @param userMap
     * @return
     * @throws IOException
     */
    @RequestMapping("/uploadUserInfo")
    public ResultVo uploadUserInfo(@RequestBody Map<String, Object> userMap) throws IOException {
        return baiDuService.uploadUserInfo(userMap);
    }


    @RequestMapping("/callbackUrl")
    public RedirectView callbackUrl(String token) throws IOException {
        log.info("callbackUrl接口被调用当前token:{}", token);
        AuthTokenStatus authRealName = authTokenStatusService.isAuthRealName(token);
        if(!ObjectUtils.isEmpty(authRealName)){
            log.info("token:{}数据库已实名认证状态，无需调用认证结果状态接口",token);
            return new RedirectView("/index.html");
        }
        JSONObject authResult = baiDuService.getAuthResult(token);
        authTokenStatusService.updateStatusByToken(token, authResult);
        return new RedirectView("/index.html");
    }

}
